#ifndef __BSP_USART_H
#define __BSP_USART_H

#include "gd32f4xx.h"
#include <stdio.h>
#include "bsp_dma.h"

#define RCU_COM_PORT_PERIPH		RCU_GPIOA
#define RCU_COM_USART_PERIPH	RCU_USART0
#define COM_PORT				GPIOA
#define COM_USART				USART0

#define COM_USART_IRQn			USART0_IRQn
#define COM_USART_IRQHandler	USART0_IRQHandler
#define COM_TX_PIN				GPIO_PIN_9
#define COM_RX_PIN				GPIO_PIN_10

#define RCU_PRINTER_PORT_PERIPH		RCU_GPIOA
#define RCU_PRINTER_USART_PERIPH	RCU_USART1
#define PRINTER_PORT				GPIOA
#define PRINTER_USART				USART1

#define PRINTER_TX_PIN				GPIO_PIN_2
#define PRINTER_RX_PIN				GPIO_PIN_3

#define COMM_PORT			0
#define PRN_PORT			1
#define IS_PORT_ID(PORT)	(((PORT) == COMM_PORT) || ((PORT) == PRN_PORT))

#define RX_BUFFER_SIZE		1024
#define TX_BUFFER_SIZE		1024
//#define USART_WITH_DMA

extern uint8_t	_TxBuffer[TX_BUFFER_SIZE];
extern uint8_t	_RxBuffer[RX_BUFFER_SIZE];
extern uint16_t	_RxBufferIndex;
extern uint8_t*	_pRxBuffer;
extern uint8_t	_fRxComplete;
extern uint8_t	_ProcessFlag;

void COM_Init(uint32_t baudi);
void COM_DeInit(void);
void Usart_SendByte(uint8_t ch);
void Usart_SendString(const char *str);
uint8_t usart_input_threshold(uint32_t value);

void Printer_USARTInit(uint32_t baudi);
void Printer_USARTDeInit(void);
void Printer_SendByte(uint8_t ch);

#ifdef USART_WITH_DMA
void Usart_SendString_DMA(const char *str);
void DMA_For_Usart0_Rx_Config(void);
void DMA_For_Usart0_Tx_Config(uint16_t length);
#endif

#endif /* __BSP_USART_H */
